import json
import socket
import time

from pylivelinkface import PyLiveLinkFace, FaceBlendShape
import numpy as np
UDP_IP = "127.0.0.1"
UDP_PORT = 11111
py_face = PyLiveLinkFace()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((UDP_IP, UDP_PORT))
face_object = [FaceBlendShape.EyeBlinkLeft,
FaceBlendShape.EyeLookDownLeft,
FaceBlendShape.EyeLookInLeft,
FaceBlendShape.EyeLookOutLeft,
FaceBlendShape.EyeLookUpLeft,
FaceBlendShape.EyeSquintLeft,
FaceBlendShape.EyeWideLeft,
FaceBlendShape.EyeBlinkRight,
FaceBlendShape.EyeLookDownRight,
FaceBlendShape.EyeLookInRight,
FaceBlendShape.EyeLookOutRight,
FaceBlendShape.EyeLookUpRight,
FaceBlendShape.EyeSquintRight,
FaceBlendShape.EyeWideRight,
FaceBlendShape.JawForward,
FaceBlendShape.JawLeft,
FaceBlendShape.JawRight,
FaceBlendShape.JawOpen,
FaceBlendShape.MouthClose,
FaceBlendShape.MouthFunnel,
FaceBlendShape.MouthPucker,
FaceBlendShape.MouthLeft,
FaceBlendShape.MouthRight,
FaceBlendShape.MouthSmileLeft,
FaceBlendShape.MouthSmileRight,
FaceBlendShape.MouthFrownLeft,
FaceBlendShape.MouthFrownRight,
FaceBlendShape.MouthDimpleLeft,
FaceBlendShape.MouthDimpleRight,
FaceBlendShape.MouthStretchLeft,
FaceBlendShape.MouthStretchRight,
FaceBlendShape.MouthRollLower,
FaceBlendShape.MouthRollUpper,
FaceBlendShape.MouthShrugLower,
FaceBlendShape.MouthShrugUpper,
FaceBlendShape.MouthPressLeft,
FaceBlendShape.MouthPressRight,
FaceBlendShape.MouthLowerDownLeft,
FaceBlendShape.MouthLowerDownRight,
FaceBlendShape.MouthUpperUpLeft,
FaceBlendShape.MouthUpperUpRight,
FaceBlendShape.BrowDownLeft,
FaceBlendShape.BrowDownRight,
FaceBlendShape.BrowInnerUp,
FaceBlendShape.BrowOuterUpLeft,
FaceBlendShape.BrowOuterUpRight,
FaceBlendShape.CheekPuff,
FaceBlendShape.CheekSquintLeft,
FaceBlendShape.CheekSquintRight,
FaceBlendShape.NoseSneerLeft,
FaceBlendShape.NoseSneerRight,
FaceBlendShape.TongueOut,
FaceBlendShape.HeadRoll,
FaceBlendShape.LeftEyeRoll,
FaceBlendShape.RightEyeRoll]
def send_weight(weight):
    inner_dict = dict(zip(face_object, weight))
    for each,value in inner_dict.items():
        py_face.set_blendshape(each,value)
        s.sendall(py_face.encode())

def send_azure_blendshape_weight2(animation,start_time,aligin=True):
    if animation.strip():
        animation=json.loads(animation)
        frameIndex=animation["FrameIndex"]
        weights = np.array(animation["BlendShapes"])

        if aligin:
            duration_time = frameIndex/60
            print("weight_num:",len(weights))
            current_time = time.time()
            sleep_time = duration_time-(current_time - start_time)
            print("duration_time",duration_time,"current spend time:",current_time - start_time)
            if sleep_time>0:
                print("begin sleep",sleep_time)
                time.sleep(sleep_time)
                speed_up = 0
            else:
                speed_up = sleep_time/(len(weights))
        else:
            speed_up=0
        print("more spped",speed_up)
        for i,weight in enumerate(weights):
            if i % 2 == 0:
                continue
            send_weight(weight)
            if 1/60+speed_up>0:
                time.sleep(1/60+speed_up)

